from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
class MyClass(object):
    def __init__(self):
        self.web = webdriver.Chrome()
        self.web.implicitly_wait(10)
        url = 'https://webvpn.nankai.edu.cn/http/77726476706e69737468656265737421f5f64c95347e6651700388a5d6502720dc08a5/eams/myPlan!programDoc.action'
        self.web.get(url)
        self.is_continue=True
    def log_in(self):
        username=input('请输入学号：')
        password=input('请输入密码：')
        self.web.find_element_by_xpath('/html/body/div/div[2]/div[1]/div[2]/ul/li[2]/input').send_keys(username)
        self.web.find_element_by_xpath('/html/body/div/div[2]/div[1]/div[2]/ul/li[3]/input').send_keys(password)

        ac = ActionChains(self.web)
        ac.drag_and_drop_by_offset(self.web.find_element_by_xpath('//*[@id="btn"]'), 250, 0).perform()
    def to_page(self):
        self.web.find_element_by_xpath('//*[@id="submitRole"]').click()
        self.web.find_element_by_xpath('//*[@id="menu_panel"]/ul/li[1]/ul/div/li[3]/a').click()
        self.web.find_element_by_xpath('//*[@id="ui-id-2"]').click()
        self.mainWindow = self.web.current_window_handle
        print('输入"over"结束程序！')
    def to_new(self):
        self.mainWindow = self.web.current_window_handle
        self.name = input('输入课程名称：')
        if self.name=='over':
            self.web.quit()
            print('完成查找！开学快乐！')
            self.is_continue=False
            return
        try:
            self.web.find_element_by_partial_link_text(self.name).click()
            self.new_window = self.web.window_handles[-1]
            self.is_pass = True
        except:
            self.is_pass = False
            print('ERROR！未查找到相关课程！请重新输入！')
    def get_info(self):
        self.to_new()
        while (self.is_continue):
            if self.is_pass:
                self.web.switch_to.window(self.new_window)
                with open('教材名查找.txt', 'a')as file:
                    # print(web.page_source)
                    try:
                        book_name=self.web.find_element_by_xpath('/html/body/table/tbody/tr[1]/td[2]').text
                        info = self.web.find_element_by_xpath('/html/body/table/tbody/tr[9]/td[2]').text
                    except:
                        #self.web.find_element_by_xpath('/html/body/table/tbody/tr[5]/td[2]/a').click()
                        info = '该课程未显示所用教材'
                    file.write(book_name + '--------所用教材：' + info + '\n')
                    file.write(
                        '.............................................................................................\n')
                self.web.close()
                self.web.switch_to.window(self.mainWindow)
            else:
                pass
            self.to_new()
    def run(self):
        self.log_in()
        self.to_page()
        self.get_info()
class_book = MyClass()
class_book.run()